package com.carrot.gateway.filter.handler.auth;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 认证校验依据api版本的工厂
 */
@Component
@AllArgsConstructor
public class AuthApiVersionHandlerFactory {

    private final static String API_VERSION_SYMBOL = "V";
    
    private final AuthHandler authNewHandler;

    public IAuthHandler getHandlerByApiVersion(String apiVersion){
        final int apiVersionLevel = this.getApiVersionLevel(apiVersion);
        return authNewHandler;
    }

    /**
     * 解析api version中的版本等级
     * 若不符合 v1 这种格式则返回0
     * @param apiVersion
     * @return
     */
    private int getApiVersionLevel(String apiVersion){
        if(StringUtils.isBlank(apiVersion)
                || apiVersion.length() < 2
                || !apiVersion.toUpperCase().startsWith(API_VERSION_SYMBOL)){
            return 0;
        }
        try{
            return Integer.parseInt(apiVersion.substring(1));
        }catch (Exception e){
            return 0;
        }
    }

}
